package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override// 2.1 查询所有护理级别
    public List<NursingLevelVo> selectAll() {
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectAll();
        List<NursingLevelVo> nursingLevelVos = BeanUtil.copyToList(nursingLevels, NursingLevelVo.class);
        return nursingLevelVos;
    }

    @Override// 2.2.1 新增护理级别
    public void insertNursingLevel(NursingLevelDto nursingLevelDTo) {
        // 重名校验
        if (nursingLevelMapper.countByName(nursingLevelDTo.getName()) > 0) {
            throw new RuntimeException("护理级别名称重复");
        }
        // 校验新增价格不超过最大值
        BigDecimal number = BigDecimal.valueOf(999999999.99);
        if (nursingLevelDTo.getFee().compareTo(number)>0){
            throw new RuntimeException("护理费用不能超过999999999.99");
        }
        nursingLevelMapper.insert(BeanUtil.copyProperties(nursingLevelDTo, NursingLevel.class));

    }

    @Override  // 2.2.2 批量新增
    public void insertBatch(List<NursingLevelDto> nursingLevelDtos) {
      List<NursingLevel> nursingLevels= new ArrayList<>();
        for (NursingLevelDto nursingLevelDto : nursingLevelDtos) {
        if (nursingLevelMapper.countByName(nursingLevelDto.getName()) > 0) {
                throw new RuntimeException("批量添加数据中存在护理级别名称重复");
            }
             nursingLevels.add(BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class));
        }
        nursingLevelMapper.insertBatch(nursingLevels);
    }

    @Override //2.3 分页查询护理等级NursingLevel
    public PageResponse<NursingLevelVo> selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> nursingLevelVos = nursingLevelMapper.selectByPage(name, status);
        return PageResponse.of(nursingLevelVos,NursingLevelVo.class);
    }

    @Override// 2.4 根据id查询护理等级Level
    public NursingLevelVo findById(Long id) {
        // 校验id是否存在
        int count=  nursingLevelMapper.CountById(id);
        if (count==0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_QUERY_FAIL_BY_ID);
        }
        NursingLevel nursingLevel = nursingLevelMapper.findById(id);
        return BeanUtil.copyProperties(nursingLevel, NursingLevelVo.class);
    }

    @Override // 2.5更新护理等级信息
    public void update(NursingLevelDto nursingLevelDto) {
        // 先校验是否存在
        int count=  nursingLevelMapper.CountById(nursingLevelDto.getId());
        if (count==0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_QUERY_FAIL_BY_ID);
        }
        // 存在进行修改
        nursingLevelMapper.update(BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class));
    }

    @Override// 2.6 删除护理等级信息
    public void delete(Long id) {
        // 先校验是否存在
        int count=  nursingLevelMapper.CountById(id);
        if (count==0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_QUERY_FAIL_BY_ID);
        }
        NursingLevel nursingLevel = nursingLevelMapper.findById(id);
        // 判断护理等级是否是禁用状态，禁用才能删除
        if(nursingLevel.getStatus()==1){
            throw new BaseException(BasicEnum.NURSING_PLAN_DELETE_FAIL_BY_STATUS);
        }
        // 存在即进行删除操作
        nursingLevelMapper.delete(id);

    }

    @Override// 2.7 启用/禁用护理等级
    public void enableOrDisable(Long id, Integer status) {
        // 先校验是否存在
        int count=  nursingLevelMapper.CountById(id);
        if (count==0) {
            throw new BaseException(BasicEnum.NURSING_LEVEL_QUERY_FAIL_BY_ID);
        }

        nursingLevelMapper.enableOrDisable(id, status);
    }

    @Override//2.8 根据nursingPlan计划id查询护理等级
    public List<NursingLevelVo> listAllByPlanIds(List<Long> ids) {
     List<NursingLevel> nursingLevels =   nursingLevelMapper.listAllByPlanIds(ids);
     List<NursingLevelVo> nursingLevelVoList= new ArrayList<>();
        for (NursingLevel nursingLevel : nursingLevels) {
            NursingLevelVo nursingLevelVo = BeanUtil.copyProperties(nursingLevel, NursingLevelVo.class);
            nursingLevelVoList.add(nursingLevelVo);
        }
        return nursingLevelVoList;
    }


}
